//
//  O(n)时间内求和为target两个数.swift
//  手撕代码
//
//  Created by xiaoZuDeMeng on 2024/1/19.
//

import Foundation

//https://leetcode.cn/problems/two-sum/description/
// 给定一个整数数组nums和一个目标target，请你在该数组中找出和为目标值的那俩个
// 整数，并返回它们的数组下标
// 示例
// nums = [2, 7, 11, 15], target = 9
// nums[0] + nums[1] = 2 + 7 = 9
// 所以返回[0, 1]

func getTargetIndexes(_ nums: [Int], target: Int) -> [Int] {
    var map: [Int: Int] = [:]
    for (index, value) in nums.enumerated() {
        let key = target - value
        if let targetIndex = map[key] {
            return [targetIndex, index]
        }
        map[value] = index
    }
    return []
}
